<template>
  <div @click="getCode()">
    <div id="img">
      <canvas id="mycanvas"></canvas>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  props: {
    refresh: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    refresh() {
      this.getCode();
    },
  },
  methods: {
    getCode() {
      var options = {
        canvasId: "mycanvas",
        width: 100,
        height: 40,
        txt: "",
      };
      this.code(options);
      this.$emit("code", options.txt);
    },
    //生产验证码
    produceCode(options) {
      var code = "";
      var random = new Array(
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9
        
      );
      for (var i = 0; i < 4; i++) {
        var index = Math.floor(Math.random() * 10);
        code += random[index];
      }
      options.txt = code;
    },

    //产生随机数字
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },

    //产生随机颜色
    randomColor(min, max) {
      var r = this.randomNum(min, max);
      var g = this.randomNum(min, max);
      var b = this.randomNum(min, max);
      return "rgb(" + r + "," + g + "," + b + ")";
    },

    //生产验证码背景
    code(options) {
      this.produceCode(options);
      var canvas = document.getElementById(options.canvasId);
      canvas.width = options.width || 300; //画布的宽
      canvas.height = options.height || 150; //画布的高
      var ctx = canvas.getContext("2d"); //创建一个canvas对象
      ctx.textBaseline = "middle";
      ctx.fillStyle = "#fff";
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      for (var i = 0; i < options.txt.length; i++) {
        var txt = options.txt.charAt(i);
        ctx.font = "50px SimHei";
        ctx.fillStyle = this.randomColor(60, 180); /**随机生成字体颜色*/
        ctx.shadowOffsetY = this.randomNum(-3, 3);
        ctx.shadowBlur = this.randomNum(-3, 3);
        ctx.shadowColor = "rgba(0, 0, 0, 0.3)";
        var x = (options.width / (options.txt.length + 1)) * (i + 0.1);
        var y = options.height / 2;
        var deg = this.randomNum(-30, 30);
        /**设置旋转角度和坐标原点*/
        ctx.translate(x, y);
        ctx.rotate((deg * Math.PI) / 180);
        ctx.fillText(txt, 0, 0);
        /**恢复旋转角度和坐标原点*/
        ctx.rotate((-deg * Math.PI) / 180);
        ctx.translate(-x, -y);
      }

      /**1~4条随机干扰线随机出现*/
      for (var i = 0; i < this.randomNum(1, 4); i++) {
        ctx.strokeStyle = this.randomColor(40, 180);
        ctx.beginPath();
        ctx.moveTo(
          this.randomNum(0, options.width),
          this.randomNum(0, options.height)
        );
        ctx.lineTo(
          this.randomNum(0, options.width),
          this.randomNum(0, options.height)
        );
        ctx.stroke();
      }
      /**绘制干扰点*/
      for (var i = 0; i < options.width / 6; i++) {
        ctx.fillStyle = this.randomColor(0, 255);
        ctx.beginPath();
        ctx.arc(
          this.randomNum(0, options.width),
          this.randomNum(0, options.height),
          1,
          0,
          2 * Math.PI
        );
        ctx.fill();
      }
    },
  },
  mounted() {
    this.getCode();
  },
};
</script>

<style lang="scss" scoped>
#mycanvas {
  border-radius: 0.1rem;
}
</style>
